﻿
//根据百度经纬度坐标获取地址信息
function getAddressInfo(lat, lng, callback) {
    url = 'http://api.map.baidu.com/geocoder/v2/?ak=UC03PvVN1AEqsb6csSzY6vIF&location=' + Number(lat) + ',' + Number(lng) + '&output=json&callback=?';
    $.getJSON(url, function (msg) {
        //回调函数
        if (callback) {
            callback(msg);
        }
    });
}
//腾讯坐标转百度坐标
//lat-经度
//lng-纬度
//callback-回调函数
function ConvertTencentToBaidu(lat, lng, callback) {
    url = 'http://api.map.baidu.com/geoconv/v1/?coords=' + Number(lat) + ',' + Number(lng) + '&from=3&to=5&output=json&ak=UC03PvVN1AEqsb6csSzY6vIF&callback=?';
    $.getJSON(url, function (msg) {
        if (callback) {
            callback(msg);
        }
    });
}

function ConvertAddrToBaidu(addr, callback) {
    url = 'http://api.map.baidu.com/geocoder/v2/?address=' + addr + '&output=json&ak=UC03PvVN1AEqsb6csSzY6vIF';
    $.getJSON(url, function (msg) {
        if (callback) {
            callback(msg);
        }
    });
}

function CodeTencentLatLng2Address(lat, lng, callback) {
    var geocoder = new qq.maps.Geocoder();
    var latlng = new qq.maps.LatLng(lat, lng);
    geocoder.getAddress(latlng);
    geocoder.setComplete(function (result) {
        if (result.type == qq.maps.ServiceResultType.GEO_INFO) {
            var geoInfo = result.detail;
            if (callback) {
                callback(geoInfo);
            }
        }
    });
}

function getWXAddress(getSucCallback) {
    var url = '/Location/GetLocationFromCatch';
    var data = {};
    data["openId"] = UserInfoMgr.getOpenId();
    NativeAjax(data, url, function (msg) {
        if (msg != null && msg != "") {
            var jsonData = JSON.parse(msg);//转换成JSON对象
            if (null != getSucCallback) {
                getSucCallback(null, null, jsonData.Key, jsonData.Value);
            }
        }
    });
}

///函数说明：点击定位到当前位置
///lat-纬度
///lng-经度
function locationPoint(callback) {
    var Latitude = AddressMgr.getWXLat();
    var Longitude = AddressMgr.getWXLog();
    CodeTencentLatLng2Address(Latitude, Longitude, function (geoInfo) {
            var address = geoInfo.address;
            var name = address;
            var nearPoi = geoInfo.nearPois;
            if (null != nearPoi) {
                var minDis = 3000;
                var minIndex = -1;
                for (index in nearPoi) {
                    if (minDis >= nearPoi[index].dist) {
                        minDis = nearPoi[index].dist;
                        minIndex = index;
                    }
                }
                if (minIndex != -1) {
                    address = nearPoi[minIndex].address + nearPoi[minIndex].name;
                    name = nearPoi[minIndex].name;
                }
            }

            AddressMgr.setWXAddr(name, address, Latitude, Longitude);
            AddressMgr.setCurAddr(name, address, Latitude, Longitude);
            if (null != callback) {
                callback();
            }        
    });

    //ConvertTencentToBaidu(Latitude, Longitude, function (msg) {
    //    if (msg.status == 0) {
    //        var myObj = eval(msg.result);
    //        var LatitudeTemp = myObj[0].x;
    //        var LongitudeTemp = myObj[0].y;
    //        //根据百度坐标获取地址信息
    //        getAddressInfo(LatitudeTemp, LongitudeTemp, function (msg) {
    //            if (msg.status == 0) {
    //                var addressName = msg.result.formatted_address;
    //                var addressInfo = msg.result.formatted_address;
    //                var myObj = eval(msg.result.poiRegions);
    //                if (myObj.length > 0) {
    //                    addressName = myObj[0].name;
    //                }
    //                AddressMgr.setWXAddr(addressName, addressInfo, Latitude, Longitude);
    //                AddressMgr.setCurAddr(addressName, addressInfo, Latitude, Longitude);
    //                //定位之后跳转到首页
    //                window.location.href = url;
    //            }
    //        });
    //    }
    //});
}

var AddressMgr = {
    addressFromWX: {
        name: "sv.add.wx.name",
        addr: "sv.add.wx.addr",
        lat: "sv.add.wx.lat",
        log: "sv.add.wx.log",
    },
    addressCur: {
        name: "sv.add.cur.name",
        addr: "sv.add.cur.addr",
        lat: "sv.add.cur.lat",
        log: "sv.add.cur.log",
    },
    serviceCity: {
        list: "sv.add.city.list",
        city: "sv.add.cur.city",
    },
    setWXAddr: function (name, addr, lat, log) {
        if (name != null) {
            setStorage(AddressMgr.addressFromWX.name, name);
        }
        if (addr != null) {
            setStorage(AddressMgr.addressFromWX.addr, addr);
        }
        setStorage(AddressMgr.addressFromWX.lat, lat);
        setStorage(AddressMgr.addressFromWX.log, log);
    },
    setCurAddr: function (name, addr, lat, log) {
        setStorage(AddressMgr.addressCur.name, name);
        setStorage(AddressMgr.addressCur.addr, addr);
        setStorage(AddressMgr.addressCur.lat, lat);
        setStorage(AddressMgr.addressCur.log, log);
    },
    useWXAddr: function () {
        var name = getStorageVal(AddressMgr.addressFromWX.name);
        setStorage(AddressMgr.addressCur.name, name);
        var addr = getStorageVal(AddressMgr.addressFromWX.addr);
        setStorage(AddressMgr.addressCur.addr, addr);
        var lat = getStorageVal(AddressMgr.addressFromWX.lat);
        setStorage(AddressMgr.addressCur.lat, lat);
        var log = getStorageVal(AddressMgr.addressFromWX.log);
        setStorage(AddressMgr.addressCur.log, log);
    },
    getWXLat: function () {
        return getStorageVal(AddressMgr.addressFromWX.lat);
    },
    getWXLog: function () {
        return getStorageVal(AddressMgr.addressFromWX.log);
    },
    getCurName: function () {
        return getStorageVal(AddressMgr.addressCur.name);
    },
    getCurAddr: function () {
        return getStorageVal(AddressMgr.addressCur.addr);
    },
    getCurLat: function () {
        return getStorageVal(AddressMgr.addressCur.lat);
    },
    getCurLog: function () {
        return getStorageVal(AddressMgr.addressCur.log);
    },
    getCityList: function () {
        return getStorageVal(AddressMgr.serviceCity.list);
    },
    setCityList: function (cityList) {
        setStorage(AddressMgr.serviceCity.list, cityList);
    },
    getCityListData: function (setCityListLayout, showCurCity) {
        var cityList = AddressMgr.getCityList();
        if (null == cityList) {
            var command = "service_area";
            doAjax(null, command, function (result) {
                if (result.result == 0) {
                    AddressMgr.setCityList(result.city);
                    cityList = AddressMgr.getCityList();
                    AddressMgr.setCurCity(cityList[0]);
                    showCurCity(cityList[0]);
                    setCityListLayout(cityList);
                }
            });
        } else {
            showCurCity(AddressMgr.getCurCity());
            setCityListLayout(cityList);
        }
    },
    setCurCity: function (curCity) {
        setStorage(AddressMgr.serviceCity.city, curCity);
    },
    getCurCity: function () {
        return getStorageVal(AddressMgr.serviceCity.city);
    },
    getGPSRequestJson: function () {
        var lat = Number(AddressMgr.getCurLat());
        var log = Number(AddressMgr.getCurLog());
        var address = AddressMgr.getCurAddr();
        var addressName = AddressMgr.getCurName();
        var GPSRequestJson = {
            "gps_info": {
                "latitude": lat,
                "longitude": log
            }, // GPS信息
            "address": address, // 地址信息
            "addr_name": addressName // 地址名称
        };
        return GPSRequestJson;
    },
};